﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace QQPimSecure
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {            
            Process process = new Process();     //创建进程对象               
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName ="cmd.exe";      //设定需要执行的命令
            startInfo.UseShellExecute = false;     //不使用系统外壳程序启动                
            startInfo.RedirectStandardInput = true;   //不重定向输入                
            startInfo.RedirectStandardOutput = true;   //重定向输出                
            startInfo.CreateNoWindow = true;     //不创建窗口  
            //startInfo.WindowStyle = ProcessWindowStyle.Hidden;  
            process.StartInfo = startInfo;
            string strDevice;
            try
            {
                if (process.Start())       //开始进程                    
                {
                    process.StandardInput.WriteLine("adb devices");
                    process.StandardInput.WriteLine("exit");
                    while (true)
                    {
                        string str = process.StandardOutput.ReadLine();
                        if(str==null)
                            break;
                        if (str == "List of devices attached ")
                        {
                            textBox1.AppendText(str + "\r\n");
                            while (true)
                            {
                                str = process.StandardOutput.ReadLine();
                                if(str=="")
                                    break;
                                textBox1.AppendText(str + "\r\n");
                                if (str.IndexOf("emulator") != -1)
                                    strDevice = str.Split(' ')[0];
                            }
                            break;
                        }
                    }
                    textBox1.AppendText(process.StandardOutput.ReadToEnd());
                    textBox1.AppendText("----------------------------------------\r\n");
                }
            }
            catch
            {
                textBox1.AppendText("出错" + "\r\n");
                textBox1.AppendText(process.StandardOutput.ReadToEnd());
                textBox1.AppendText("----------------------------------------\r\n");
            }
            finally
            {
                if (process != null)
                    process.Close();
            }
        }
    }
}
